package model.action.order_deal_action;

//import java.util.List;
import java.util.ArrayList;

import model.dap.dao.DealDAO;
import model.dap.dao.HistoryDAO;
import model.dap.domain.Consumer;
import model.dap.domain.Deal;
import model.dap.domain.OrderHistory;
import model.dap.mapper.DealMapper;
import model.dap.mapper.HistoryMapper;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionContext;
import common.base.BaseAction;
//import com.opensymphony.xwork2.ModelDriven;

@Results({
	@Result(name = "orderHistoryload", type = "tiles", location = "tiles.OrderDealHistory"),
	@Result(name = "editorder", type = "tiles", location = "tiles.EditOrder"),
	@Result(name = "success", type = "tiles", location = "tiles.OrderDealHistory"),
	@Result(name = "errorex", type = "tiles", location = "tiles.OrderDealHistory"),
	@Result(name = "error", type = "tiles", location = "tiles.error"),
	@Result(name = "successCx", type="redirectAction", location="order-history.html")
})

@ParentPackage("master_login")
public class OrderedHistoryAction extends BaseAction{	
	private String userID;
	
	private String selected;
	
	private int orderID;

	private ArrayList<OrderHistory> history_li;
	
	private String orderNumber;


	public ArrayList<OrderHistory> getHistory_li() {
		return history_li;
	}

	public void setHistory_li(ArrayList<OrderHistory> history_li) {
		this.history_li = history_li;
	}

	private String contextPath = null;
	@Override
	public String getContextPath() {
		return contextPath;
	}
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}
	
	public String getUserID() {
		//userID = ServletActionContext.getRequest().getParameter("userID");
		userID = ((Consumer) ActionContext.getContext().getSession().get("master_user")).getUserID();
		return userID;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}

	public ArrayList<OrderHistory> getHistory() {
		return history_li;
	}
	public void setHistory(ArrayList<OrderHistory> historyList) {
		this.history_li = historyList;
	}

	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	
	public String getSelected() {
		selected = ServletActionContext.getRequest().getParameter("selected");
		return selected;
	}
	public void setSelected(String selected) {
		this.selected = selected;
	}
	
	public int getOrderID() {
		orderID = Integer.parseInt(ServletActionContext.getRequest().getParameter("orderID"));
		return orderID;
	}
	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}
	
	int numberOrder;
	public int getNumberOrder(){
		String no = ServletActionContext.getRequest().getParameter("numberOrder");
		return Integer.parseInt(no);
	}
	public void setNumberOrder(int numberorder){
		this.numberOrder = numberorder;
	}
	
	protected static Logger log = Logger.getLogger(OrderedHistoryAction.class);
	private static final long serialVersionUID = 1L;

	
	// get history by consumerID - ALL
	@Action("/master/order-history")
	public String order_history() {
		try {
			getCityList2();
			loadParentDealCategory();
			HistoryMapper historymapper = new HistoryDAO();
			history_li = historymapper.selectHistoryFromDB(getCurrentUser().getUserID());
			for(int i=0; i<history_li.size();i++){
				if(history_li.get(i).getCurrencyID().equals("vnd")){
					history_li.get(i).setNewTotalMoney(newMoney(history_li.get(i).getNewTotalMoney()+""));					
				}
				else{
					history_li.get(i).setNewTotalMoney(history_li.get(i).getNewTotalMoney()+"");	
				}
			}
			// Luu lai so luong order dc hien thi
			orderNumber = "" + history_li.size();

			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
		
	}
	
	// get history by consumerID - Top 10 Row
	@Action("/master/order-history-10")
	public String order_history_10() {
		try {
			getCityList2();
			loadParentDealCategory();
			HistoryMapper historymapper = new HistoryDAO();
			history_li = historymapper.selectHistoryFromDB_10(getCurrentUser()
					.getUserID());
			for(int i=0; i<history_li.size();i++){
				if(history_li.get(i).getCurrencyID().equals("vnd")){
					history_li.get(i).setNewTotalMoney(newMoney(history_li.get(i).getNewTotalMoney()+""));					
				}
				else{
					history_li.get(i).setNewTotalMoney(history_li.get(i).getNewTotalMoney()+"");	
				}
			}
			// Luu lai so luong order dc hien thi
			orderNumber = "" + history_li.size();

			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
	}

	// get history by consumerID - Top 20 Row
	@Action("/master/order-history-20")
	public String order_history_20() {
		try {
			getCityList2();
			loadParentDealCategory();
			HistoryMapper historymapper = new HistoryDAO();
			history_li = historymapper.selectHistoryFromDB_20(getCurrentUser()
					.getUserID());
			for(int i=0; i<history_li.size();i++){
				if(history_li.get(i).getCurrencyID().equals("vnd")){
					history_li.get(i).setNewTotalMoney(newMoney(history_li.get(i).getNewTotalMoney()+""));					
				}
				else{
					history_li.get(i).setNewTotalMoney(history_li.get(i).getNewTotalMoney()+"");	
				}
			}
			// Luu lai so luong order dc hien thi
			orderNumber = "" + history_li.size();

			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
	}
	
	// get history by consumerID - Top 30 Row
	@Action("/master/order-history-30")
	public String order_history_30() {
		try {
			getCityList2();
			loadParentDealCategory();
			HistoryMapper historymapper = new HistoryDAO();
			history_li = historymapper.selectHistoryFromDB_30(getCurrentUser()
					.getUserID());
			for(int i=0; i<history_li.size();i++){
				if(history_li.get(i).getCurrencyID().equals("vnd")){
					history_li.get(i).setNewTotalMoney(newMoney(history_li.get(i).getNewTotalMoney()+""));					
				}
				else{
					history_li.get(i).setNewTotalMoney(history_li.get(i).getNewTotalMoney()+"");	
				}
			}
			// Luu lai so luong order dc hien thi
			orderNumber = "" + history_li.size();

			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
	}
	
	//Cancel deal
	@Action("/master/cancel-orderdeal")
	public String cancel_orderdeal(){
		try{
			String type = ServletActionContext.getRequest().getMethod();
			
			if(type.toUpperCase() == "GET")
			{
				return "successCx";
			}
		getCityList2();
		loadParentDealCategory();
		HistoryMapper historymapper = new HistoryDAO();		
		OrderHistory order = historymapper.selectOrderHistory(getOrderID());
		//OrderHistory order = historymapper.selectOrderHistory(orderID);
			
		//Change status order
		order.setOrderStatusID(2);
		
		//Update status into database
		int cons1 = historymapper.updateStatusOrderDeal(order);
		if(cons1 == 0) {
			showError("Có lỗi về tình trạng deal, Liên hệ với admin để biết thêm về tình trạng này");
			history_li = historymapper.selectHistoryFromDB(getCurrentUser().getUserID());
			orderNumber="" + history_li.size();
			return "errorex";
		}
		//historymapper.updateStatusOrderDeal(order);
		
		//Change & Update deal number
		DealMapper dealmapper = new DealDAO();
		Deal deal = dealmapper.selectDeal(order.getDealID());
		deal.setTotalAmount(deal.getTotalAmount()+order.getNumberOrder());
		deal.setNumberOrdered(deal.getNumberOrdered()-order.getNumberOrder());
		//Update number order into database
		int cons = dealmapper.updateNumberOrderDeal(deal);
		if(cons == 0) {
			showError("Có lỗi về tình trạng deal, Liên hệ với admin để biết thêm về tình trạng này");
			history_li = historymapper.selectHistoryFromDB(getCurrentUser().getUserID());
			orderNumber="" + history_li.size();
			return "errorex";
		}
		//dealmapper.updateNumberOrderDeal(deal);
		
		history_li = historymapper.selectHistoryFromDB(getCurrentUser().getUserID());
		orderNumber="" + history_li.size();
		
		return SUCCESS;
		}
		catch(Exception e) {
			log.error(e.getMessage());
			return ERROR;

		}
	}
}
